package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LeetCode39 {

    public static void main(String[] args) {
        LeetCode39 leetCode39 = new LeetCode39();
        System.out.println(leetCode39.combinationSum(new int[]{2, 3, 6, 7}, 7));
    }

    int[] dict;
    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        dict = candidates;
        Arrays.sort(dict);
        calculation(target, 0, new ArrayList<>());
        return result;
    }

    private void calculation(int target, int minIndex, List<Integer> lastList) {
        if (target == 0) {
            result.add(lastList);
            return;
        }
        if (target < dict[0])
            return;
        for (int i = minIndex; i < dict.length; i++) {
            if (dict[i] > target)
                break;
            List<Integer> list = new ArrayList<>(lastList);
            list.add(dict[i]);
            calculation(target - dict[i], i, list);
        }
    }
//------------------------------------------------------------------
}
